import { combineReducers, configureStore,ThunkAction,Action } from "@reduxjs/toolkit";
import { PostListSlice } from "./postList";
import { UserInfoSlice } from "./userInfo";
import thunkMiddleware from "redux-thunk";
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import { PostDetailSlice } from "./postDetail";
import { CurrentPost } from "./currentPost";
import { VideoDataSlice } from "./videoData";
import { GlobalDataSlice } from "./globalData";
import { MediaListSlice } from "./mediasList";
import { AblumnCategoriesSlice } from "./ablumCategories";
// 合并多个reducer
const rootReducer = combineReducers({
  postList: PostListSlice.reducer,
  userInfo:UserInfoSlice.reducer,
  postDetail: PostDetailSlice.reducer,
  currentPost:CurrentPost.reducer,
  videoData:VideoDataSlice.reducer,
  globalData:GlobalDataSlice.reducer,
  mediaList: MediaListSlice.reducer,
  ablumnCategories:AblumnCategoriesSlice.reducer
});
const store = configureStore({
  reducer: rootReducer,
  // 可以添加自己的中间件,比如打印日志的
  middleware: (getDefaultMiddleware)=>getDefaultMiddleware().concat([thunkMiddleware]),
  devTools: true,
});

// 获取全部store数据类型
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;
export type AppThunk<ReturnType = void> = ThunkAction<
ReturnType,
RootState,
unknown,
Action<string>
>;
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
export const useAppDispatch: () => AppDispatch = useDispatch;

export default store;
